﻿using MediatR;
using Swork03_28.Api.Application.Commands.TraceableCommand;
using Swork03_28.Domain;
using Swork03_28.Domain.TraceableInfos;
using Swork03_28.Services.Interface;

namespace Swork03_28.Api.Application.CommandHandlers.TraceableHandler
{
    public class TraceableCreateHandler : IRequestHandler<TraceableCreateCommand, int>
    {
        private readonly ITraceableService traceableService;
        private readonly IGoodsService goodsService;

        public TraceableCreateHandler(ITraceableService traceableService, IGoodsService goodsService)
        {
            this.traceableService = traceableService;
            this.goodsService = goodsService;
        }

        public async Task<int> Handle(TraceableCreateCommand request, CancellationToken cancellationToken)
        {
            var only = traceableService.GetValues(x => x.Batch == request.Batch).Result.FirstOrDefault();
            if (only != null)
            {
                //该批次号已存在
                return -1;
            }
            return await traceableService.CreateAsync(new Traceables()
            {
                Batch = request.Batch,
                Gid = request.Gid,
                Id = request.Id,
                Images = request.Images,
                IsDel = false,
                Number = request.Number,
                ProduceEnterprise = request.ProduceEnterprise,
                Shelflife = request.Shelflife,
                CreatePeople = "张三",
                CreateTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                UpdateePeople = null,
                UpdateTime = null,
            });
        }
    }
}
